home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / List.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  7.3 KB  |  261 lines

  1. package symantec.itools.db.awt;
  2.  
  3. import java.awt.Event;
  4. import java.io.IOException;
  5. import java.util.Enumeration;
  6. import java.util.Vector;
  7. import symantec.itools.db.pro.ConnectionInfo;
  8. import symantec.itools.db.pro.ListBinder;
  9. import symantec.itools.db.pro.ListLink;
  10. import symantec.itools.db.pro.ProjBinder;
  11. import symantec.itools.db.pro.ProjLink;
  12. import symantec.itools.db.pro.RelationView;
  13. import symjava.sql.SQLException;
  14.  
  15. public class List extends java.awt.List implements ListLink, ProjLink {
  16.    private ListBinder m_ListBinder;
  17.    private ProjBinder m_ProjBinder;
  18.    private boolean m_DynamicUpdate;
  19.    private String m_BinderData;
  20.    private String m_ScreenData;
  21.    private boolean m_useStream;
  22.    private boolean m_IsBound;
  23.    private int m_treatBlankAs;
  24.    private RelationView m_LookUpRelationView;
  25.    private Vector m_JoinColumns;
  26.    private ConnectionInfo m_Conn;
  27.    private String m_SQL;
  28.    private Vector staticItems;
  29.  
  30.    public List(RelationView lookupRV, ConnectionInfo conn) {
  31.       this();
  32.       this.m_LookUpRelationView = lookupRV;
  33.       this.m_Conn = conn;
  34.    }
  35.  
  36.    public List(int rows) {
  37.       super(rows, false);
  38.       this.m_DynamicUpdate = false;
  39.       this.m_useStream = false;
  40.       this.m_IsBound = false;
  41.       this.staticItems = new Vector();
  42.       this.m_ScreenData = new String();
  43.       this.m_BinderData = new String();
  44.       this.m_JoinColumns = new Vector();
  45.       this.m_SQL = new String();
  46.    }
  47.  
  48.    public List() {
  49.       this(0);
  50.    }
  51.  
  52.    public void init(ListBinder lBinder) {
  53.       this.m_ListBinder = lBinder;
  54.    }
  55.  
  56.    public void notifyListChange(ListBinder lBinder) {
  57.       try {
  58.          RelationView curRV = lBinder.getRelationView();
  59.          ((java.awt.List)this).clear();
  60.  
  61.          while(curRV.next()) {
  62.             super.addItem(curRV.getString(1));
  63.          }
  64.  
  65.          Enumeration e = this.staticItems.elements();
  66.  
  67.          while(e.hasMoreElements()) {
  68.             super.addItem((String)e.nextElement());
  69.          }
  70.  
  71.          if (this.m_IsBound) {
  72.             this.notifyDataChange(this.m_ProjBinder);
  73.             return;
  74.          }
  75.       } catch (SQLException Ex) {
  76.          this.raiseException("Exception from List.notifyDataChange.setString: " + ((Throwable)Ex).getMessage());
  77.       }
  78.  
  79.    }
  80.  
  81.    public void init(ProjBinder binder) {
  82.       this.m_ProjBinder = binder;
  83.       this.setEditable(this.m_ProjBinder);
  84.    }
  85.  
  86.    public void setTreatBlankAs(String blank) {
  87.       if ((new String(blank)).toUpperCase().equals("DEFAULT")) {
  88.          this.m_treatBlankAs = 0;
  89.       } else if ((new String(blank)).toUpperCase().equals("NULL")) {
  90.          this.m_treatBlankAs = 1;
  91.       } else {
  92.          if ((new String(blank)).toUpperCase().equals("EMPTY")) {
  93.             this.m_treatBlankAs = 2;
  94.          }
  95.  
  96.       }
  97.    }
  98.  
  99.    public void setBinding(RelationView relView, String projection) {
  100.       try {
  101.          int projectionNumber = relView.findProjByName(projection);
  102.          relView.bindProj(projectionNumber, this);
  103.       } catch (SQLException Ex) {
  104.          this.raiseException("SQLException from TextField.notifyDataChange.getStringValue: " + ((Throwable)Ex).getMessage());
  105.          return;
  106.       }
  107.  
  108.       this.m_IsBound = true;
  109.    }
  110.  
  111.    public void setDynamicUpdate(boolean update) {
  112.       this.m_DynamicUpdate = update;
  113.    }
  114.  
  115.    public void notifyDataChange(ProjBinder binder) {
  116.       if (binder != null) {
  117.          this.m_ProjBinder = binder;
  118.  
  119.          try {
  120.             if (binder.getRelationView().getCurrentRecordState() != 105) {
  121.                if (binder.isReadable() && !binder.isNull()) {
  122.                   this.m_BinderData = binder.getStringValue();
  123.                } else {
  124.                   this.m_BinderData = "";
  125.                }
  126.             } else {
  127.                this.m_BinderData = "";
  128.             }
  129.          } catch (SQLException Ex) {
  130.             this.raiseException("SQLException from TextField.notifyDataChange.getStringValue: " + ((Throwable)Ex).getMessage());
  131.          } catch (IOException Ex) {
  132.             this.raiseException("IOException from TextField.notifyDataChange.getStringValue: " + ((Throwable)Ex).getMessage());
  133.          }
  134.  
  135.          if (this.m_BinderData == null) {
  136.             this.m_BinderData = new String();
  137.          }
  138.  
  139.          this.m_ScreenData = this.getText();
  140.          if (!this.m_BinderData.equals(this.m_ScreenData)) {
  141.             this.m_ScreenData = this.m_BinderData;
  142.             this.setText(this.m_ScreenData);
  143.          }
  144.  
  145.          this.setEditable(this.m_ProjBinder);
  146.       }
  147.    }
  148.  
  149.    public boolean lostFocus(Event evt, Object what) {
  150.       if (this.m_IsBound) {
  151.          this.notifySetData(this.m_ProjBinder);
  152.       }
  153.  
  154.       return super.lostFocus(evt, what);
  155.    }
  156.  
  157.    boolean notifyInputChanged(ProjBinder binder, String input) {
  158.       if (!this.m_ScreenData.equals(input)) {
  159.          this.m_ScreenData = input;
  160.       }
  161.  
  162.       if (!this.m_BinderData.equals(this.m_ScreenData)) {
  163.          this.m_BinderData = this.m_ScreenData;
  164.  
  165.          try {
  166.             binder.setValueFromString(this.m_ScreenData, 0, this.m_treatBlankAs);
  167.          } catch (SQLException Ex) {
  168.             this.raiseException("SQLException from List.notifyInputChange: " + ((Throwable)Ex).getMessage());
  169.             return false;
  170.          } catch (IOException Ex) {
  171.             this.raiseException("IOException from List.notifyInputChange: " + ((Throwable)Ex).getMessage());
  172.             return false;
  173.          }
  174.       }
  175.  
  176.       return true;
  177.    }
  178.  
  179.    public boolean notifySetData(ProjBinder binder) {
  180.       return this.notifyInputChanged(binder, this.getText());
  181.    }
  182.  
  183.    public boolean handleEvent(Event evt) {
  184.       if (evt.id == 701 && this.m_IsBound) {
  185.          this.notifySetData(this.m_ProjBinder);
  186.       }
  187.  
  188.       return super.handleEvent(evt);
  189.    }
  190.  
  191.    void setText(String text) {
  192.       boolean bitemFound = false;
  193.  
  194.       for(int index = 0; index < ((java.awt.List)this).countItems(); ++index) {
  195.          if (text.equals(((java.awt.List)this).getItem(index))) {
  196.             ((java.awt.List)this).select(index);
  197.             ((java.awt.List)this).makeVisible(index);
  198.             bitemFound = true;
  199.             return;
  200.          }
  201.  
  202.          if (!bitemFound && ((java.awt.List)this).getSelectedIndex() != -1) {
  203.             ((java.awt.List)this).deselect(((java.awt.List)this).getSelectedIndex());
  204.          }
  205.       }
  206.  
  207.    }
  208.  
  209.    void setEditable(ProjBinder binder) {
  210.       boolean isWritable = false;
  211.  
  212.       try {
  213.          if (binder != null) {
  214.             RelationView rv = binder.getRelationView();
  215.             if (rv != null && rv.getCurrentRecordState() != 105) {
  216.                isWritable = binder.isWritable();
  217.                return;
  218.             }
  219.          }
  220.       } catch (SQLException Ex) {
  221.          this.raiseException(((Throwable)Ex).getMessage());
  222.       }
  223.  
  224.    }
  225.  
  226.    String getText() {
  227.       String[] selectedItems = ((java.awt.List)this).getSelectedItems();
  228.       return selectedItems.length == 1 ? selectedItems[0] : "";
  229.    }
  230.  
  231.    void raiseException(String text) {
  232.       System.out.println(text);
  233.    }
  234.  
  235.    public void join(String columnName) throws SQLException {
  236.       int colID = this.m_LookUpRelationView.findProjByName(columnName);
  237.       if (this.m_JoinColumns == null) {
  238.          this.m_JoinColumns = new Vector();
  239.       }
  240.  
  241.       this.m_JoinColumns.addElement(new Integer(colID));
  242.    }
  243.  
  244.    public void setSQL(String sql) throws SQLException {
  245.       this.m_SQL = sql;
  246.    }
  247.  
  248.    public void bindList() throws SQLException {
  249.       this.m_LookUpRelationView.bindList(this.m_Conn, this.m_SQL, this, this.m_JoinColumns);
  250.    }
  251.  
  252.    public synchronized void addItem(String item) {
  253.       super.addItem(item, -1);
  254.       this.staticItems.addElement(item);
  255.       if (this.m_IsBound) {
  256.          this.notifyDataChange(this.m_ProjBinder);
  257.       }
  258.  
  259.    }
  260. }
  261.